package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Notice;
import cn.edu.ctbu.lddweb.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 教学通知Controller
 */
@Controller
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    //后台管理系统
    @RequestMapping("/notice/index")
    public String index(Model model, ModelMap map,Notice notice){
        List<Notice> class1 = noticeService.selectList(notice);
        model.addAttribute("notice",class1);
        return "/notice/index";
    }

    //搜索/重置
    @RequestMapping("/selectNotice")
    public String select(@RequestParam(value = "title",required = false) String title, Model model){
        Notice notice = new Notice();
        notice.setTitle(title);
        List<Notice> list = noticeService.selectList(notice);
        model.addAttribute("notice",list);
        return "/notice/index";
    }

    //新增页面
    @RequestMapping("/notice/insert")
    public String insert(){
        return "/notice/insert";
    }

    //新增操作
    @RequestMapping("/insertNotice")
    public String insertNotice(Notice notice){
        //格式化当前时间进行格式化
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义新的日期格式
        String dateString = formatter.format(date);
        notice.setDate(dateString);
        noticeService.insertNotice(notice);
        return "/notice/index";
    }


    //修改
    @RequestMapping("/notice/edit")
    public String edit(int id,Model model){
        Notice notice = noticeService.findID(id);
        model.addAttribute("notice",notice);
        return "/notice/edit";
    }

    //修改操作
    @RequestMapping("/updateNotice")
    public String updateNotice(Notice notice){
        //获取当前时间进行格式化
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义新的日期格式
        String dateString = formatter.format(date);
        notice.setDate(dateString);
        noticeService.updateNotice(notice);
        return "/notice/index";
    }

    //删除
    @RequestMapping("/deleteNotice")
    public String deleteNotice(int id){
        noticeService.deleteNotice(String.valueOf(id));
        return "/notice/index";
    }
}
